package cn.edu.hstc.wm.service;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.util.Log;
import android.util.Xml;
import cn.edu.hstc.wm.ui.UserFeedback;
import cn.edu.hstc.wm.ui.UserFeedback.Record;

/**
 * @ClassName: FeedbackService
 * @Description: 用户反馈
 * 
 * @ModificationHistory
 */
public class FeedbackService {

	private static final int SUCCESS = 0;
	private static final int FAILURE = 1;

	/**
	 * 发送反馈内容到服务器
	 * 
	 * @param content
	 *            反馈内容
	 * @param userContack
	 *            联系人方式
	 * @param time
	 *            反饋時間
	 * @return
	 */
	public static int sendFeedbackMessage(Record record) {

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 本地保存反饋記錄
	 * 
	 * @param content
	 * @param time
	 * @return
	 * @throws IOException
	 * @throws IllegalStateException
	 * @throws IllegalArgumentException
	 */
	public static boolean saveRecord(Context context, Record record) {
		FileOutputStream fos = null;
		try {
			fos = context.openFileOutput("records.xml", Context.MODE_APPEND); // 尾部添加数据
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		try {
			saveRecord2XML(record, fos);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			return false;
		} catch (IllegalStateException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (fos != null) {
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}

		}
		return true;
	}

	/**
	 * 取出本地保存記錄
	 * 
	 * @return
	 * @throws XmlPullParserException
	 * @throws IOException
	 */
	public static List<Map<String, String>> getRecord(Context context) {
		List<Map<String, String>> list = null;
		FileInputStream in = null;

		try {
			in = new FileInputStream(context.getFileStreamPath("records.xml"));
			readXML(in, list);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}

		return list;
	}

	public static void readXML(InputStream in, List<Map<String, String>> list) throws XmlPullParserException, IOException {
		Record record = null;

		XmlPullParser pullParser = Xml.newPullParser();
		pullParser.setInput(in, "UTF-8"); // 为Pull解释器设置要解析的XML数据
		int event = pullParser.getEventType();

		while (event != XmlPullParser.END_DOCUMENT) {

			switch (event) {

			case XmlPullParser.START_DOCUMENT:
				Log.d("readXML", " XmlPullParser.START_DOCUMENT");

				list = new ArrayList<Map<String, String>>();
				break;
			case XmlPullParser.START_TAG:
				Log.d("readXML", " XmlPullParser.START_TAG");

				if ("record".equals(pullParser.getName())) {
					Log.d("readXML", " id");
					int id = Integer.valueOf(pullParser.getAttributeValue(0));

					record = new UserFeedback.Record();
					record.setId(id);
				}
				if ("time".equals(pullParser.getName())) {
					Log.d("readXML", " time");
					String time = pullParser.nextText();
					record.setTime(time);
				}
				if ("contact".equals(pullParser.getName())) {
					Log.d("readXML", " contact");
					String contact = pullParser.nextText();
					record.setContact(contact);
				}
				if ("content".equals(pullParser.getName())) {
					Log.d("readXML", " content");
					String content = pullParser.nextText();
					record.setContent(content);
				}
				break;

			case XmlPullParser.END_TAG:
				Log.d("", "XmlPullParser.END_TAG");

				if ("record".equals(pullParser.getName())) {
					Map<String, String> map = new HashMap<String, String>();
					map.put("content", record.getContent());
					map.put("time", record.getTime());

					list.add(map);

					record = null;
				}
				break;

			}

			event = pullParser.next();
		}
	}

	public static void save(Record record, XmlSerializer serializer) throws IllegalArgumentException, IllegalStateException, IOException {
		serializer.startDocument("UTF-8", true);

		// 开始
		serializer.startTag(null, "records");

		serializer.startTag(null, "record");
		serializer.attribute(null, "id", record.getId() + "");

		// 时间
		serializer.startTag(null, "time");
		serializer.text(record.getTime());
		serializer.endTag(null, "time");

		// 联系人
		serializer.startTag(null, "contact");
		serializer.text(record.getContact());
		serializer.endTag(null, "contact");

		// 内容
		serializer.startTag(null, "content");
		serializer.text(record.getContent());
		serializer.endTag(null, "content");

		// 结束
		serializer.endTag(null, "record");

		serializer.endTag(null, "records");
		serializer.endDocument();
	}

	/**
	 * 保存一条记录
	 * 
	 * @param record
	 * @param out
	 * @throws IllegalArgumentException
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	public static void saveRecord2XML(Record record, OutputStream out) throws IllegalArgumentException, IllegalStateException, IOException {
		XmlSerializer serializer = Xml.newSerializer();
		serializer.setOutput(out, "UTF-8");
		save(record, serializer);
		out.flush();
		out.close();
	}

	/**
	 * 保存数据到xml文件中
	 * 
	 * @param records
	 * @param out
	 * @throws Exception
	 */
	public static void saveList(List<Record> records, OutputStream out) throws Exception {
		XmlSerializer serializer = Xml.newSerializer();
		serializer.setOutput(out, "UTF-8");
		serializer.startDocument("UTF-8", true);
		// 开始
		serializer.startTag(null, "records");
		for (Record record : records) {
			serializer.startTag(null, "record");
			serializer.attribute(null, "id", record.getId() + "");

			// 时间
			serializer.startTag(null, "time");
			serializer.text(record.getTime());
			serializer.endTag(null, "time");

			// 联系人
			serializer.startTag(null, "contact");
			serializer.text(record.getContact());
			serializer.endTag(null, "contact");

			// 内容
			serializer.startTag(null, "content");
			serializer.text(record.getContent());
			serializer.endTag(null, "content");

			serializer.endTag(null, "person");
		}
		serializer.endTag(null, "persons");
		serializer.endDocument();
		out.flush();
		out.close();
	}
}
